package com.microfch.v;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.microfch.m.DataCenter;
import com.microfch.m.Topic;
import com.microfch.ninetypetest.R;

import java.util.ArrayList;

public class ResultActivity extends AppCompatActivity implements OnChartValueSelectedListener {
    private RadarChart chart;
    private HorizontalBarChart horizon_chart;
    protected Typeface tfLight;
    public final String TAG = this.getClass().getSimpleName();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        tfLight = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");
        initRadarChar();
        initHorizonChart();
    }

    private void initRadarChar() {
        chart = findViewById(R.id.chart1);
        chart.setBackgroundColor(getColor(R.color.colorBG));

        chart.getDescription().setEnabled(false);

        chart.setWebLineWidth(1f);
        chart.setWebColor(Color.LTGRAY);
        chart.setWebLineWidthInner(1f);
        chart.setWebColorInner(Color.LTGRAY);
        chart.setWebAlpha(100);

        MarkerView mv = new RadarMarkerView(this, R.layout.radar_markerview);
        mv.setChartView(chart); // For bounds control
        chart.setMarker(mv); // Set the marker to the chart
        TextView tv = findViewById(R.id.textView);
        Intent intent = getIntent();
        tv.setText(intent.getStringExtra("res"));

        setData();

        chart.animateXY(500, 500, Easing.EaseInOutQuad);

        XAxis xAxis = chart.getXAxis();
        xAxis.setTypeface(tfLight);
        xAxis.setTextSize(12f);
        xAxis.setYOffset(0f);
        xAxis.setXOffset(0f);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            DataCenter m=DataCenter.getInstance();

            private final String[] mActivities = m.getTopics();

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return mActivities[(int) value % mActivities.length];
            }
        });
        xAxis.setTextColor(Color.WHITE);

        YAxis yAxis = chart.getYAxis();
        yAxis.setTypeface(tfLight);
        yAxis.setLabelCount(4, false);
        yAxis.setTextSize(12f);
        yAxis.setAxisMinimum(0f);
        yAxis.setAxisMaximum(18f);
        yAxis.setDrawLabels(false);

        Legend l = chart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setTypeface(tfLight);
        l.setXEntrySpace(12f);
        l.setYEntrySpace(12f);
        l.setFormSize(18f);
        l.setTextSize(22f);
        l.setTextColor(Color.WHITE);
    }

    private void initHorizonChart() {
        horizon_chart=findViewById(R.id.horizon_chart);
        horizon_chart.setOnChartValueSelectedListener(this);
        // chart.setHighlightEnabled(false);
        horizon_chart.setDrawBarShadow(false);
        horizon_chart.setDrawValueAboveBar(true);
        horizon_chart.getDescription().setEnabled(false);
        // if more than 60 entries are displayed in the chart, no values will be
        // drawn
        horizon_chart.setMaxVisibleValueCount(20);
        // scaling can now only be done on x- and y-axis separately
        horizon_chart.setPinchZoom(false);
        // draw shadows for each bar that show the maximum value
        // chart.setDrawBarShadow(true);
        horizon_chart.setDrawGridBackground(false);
        XAxis xl = horizon_chart.getXAxis();
        xl.setPosition(XAxis.XAxisPosition.TOP);
        xl.setTypeface(tfLight);
        xl.setDrawAxisLine(false);
        xl.setDrawGridLines(false);
      //  xl.setGranularity(10f);
        xl.setEnabled(false);
       // xl.setDrawLabels(true);

        YAxis yl = horizon_chart.getAxisLeft();
        yl.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        yl.setTypeface(tfLight);
        yl.setDrawAxisLine(true);
        yl.setDrawGridLines(false);
        yl.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        yl.setEnabled(false);
       // yl.setDrawLabels(true);
//        yl.setInverted(true);

        YAxis yr = horizon_chart.getAxisRight();
        yr.setTypeface(tfLight);
        yr.setDrawAxisLine(false);
        yr.setDrawGridLines(false);
        yr.setEnabled(false);
        yr.setAxisMinimum(0f); // this replaces setStartAtZero(true)
     //   yr.setInverted(true);

        horizon_chart.setFitBars(true);
        horizon_chart.animateY(2500);
        setHorizonData();
    }

    private void setHorizonData() {

        float barWidth = 4f;
        float spaceForBar = 5f;
        ArrayList<BarEntry> values = new ArrayList<>();
        int i= 0;
        for(Topic topic:DataCenter.getInstance().getTopicList()) {
            int count =  topic.getAnswerCount();
            float val = (float) (count);
            values.add(new BarEntry(i * spaceForBar, val));
            i++;
        }

        BarDataSet set1;
        set1 = new BarDataSet(values, "统计结果");

        set1.setDrawIcons(false);
        int color = getColorByType(yourType);
        set1.setBarBorderColor(color);
        set1.setBarShadowColor(color);
        String[] mActivities = DataCenter.getInstance().getTopics();
        int[] colors ={getColor(R.color.colorWhite),getColor(R.color.colorGreen),getColor(R.color.colorBlack),getColor(R.color.colorPuple),getColor(R.color.colorGray),
                getColor(R.color.colorPrimary),getColor(R.color.colorOrange),getColor(R.color.colorRed),getColor(R.color.colorYellow)};
        set1.setStackLabels(mActivities);
        set1.setColors(colors);

        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);


        BarData data = new BarData(dataSets);
        data.setValueTextSize(10f);
        data.setValueTypeface(tfLight);
        data.setBarWidth(barWidth);
        horizon_chart.setData(data);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.move_left_in_activity, R.anim.move_right_out_activity);
    }
    String yourType="九型性格";
    private void setData() {

        float mul = 20;
        float min = 0;
        int cnt = DataCenter.getInstance().getTopics().length;

        ArrayList<RadarEntry> entries1 = new ArrayList<>();
        ArrayList<RadarEntry> entries2 = new ArrayList<>();

        // NOTE: The order of the entries when being added to the entries array determines their position around the center of
        // the chart.
        int max = 0;

        Topic yourTopic = null;
        for(Topic topic:DataCenter.getInstance().getTopicList()){
           int count =  topic.getAnswerCount();
            if(count > max ){
                yourType = topic.getName();
                max = count;
                yourTopic = topic;
            }
            entries1.add(new RadarEntry(count));
        }
        setTitle(yourType);
        TextView tv = findViewById(R.id.textView);
        Log.d(TAG,"print suggestion"+yourTopic.getSugestion());
        tv.setText(yourTopic.getSugestion());

        RadarDataSet set1 = new RadarDataSet(entries1, yourType);
        int color = getColorByType(yourType);
        tv.setTextColor(color);
        set1.setColor(color);
        set1.setFillColor(color);
        set1.setDrawFilled(true);
        set1.setFillAlpha(180);
        set1.setLineWidth(2f);
        set1.setDrawHighlightCircleEnabled(true);
        set1.setDrawHighlightIndicators(false);

//        RadarDataSet set2 = new RadarDataSet(entries2, "This Week");
//        set2.setColor(Color.rgb(121, 162, 175));
//        set2.setFillColor(Color.rgb(121, 162, 175));
//        set2.setDrawFilled(true);
//        set2.setFillAlpha(180);
//        set2.setLineWidth(2f);
//        set2.setDrawHighlightCircleEnabled(true);
//        set2.setDrawHighlightIndicators(false);

        ArrayList<IRadarDataSet> sets = new ArrayList<>();
        sets.add(set1);
       // sets.add(set2);

        RadarData data = new RadarData(sets);
        data.setValueTypeface(tfLight);
        data.setValueTextSize(8f);
        data.setDrawValues(true);
        data.setValueTextColor(Color.WHITE);

        chart.setData(data);
        chart.invalidate();
    }

    private int getColorByType(String yourType) {
        if("完美型".equals(yourType)){
            return getColor(R.color.colorWhite);
        }else if("助人型".equals(yourType)){
            return getColor(R.color.colorGreen);
        }
        else if("成就型".equals(yourType)){
            return getColor(R.color.colorBlack);
        }else if("自我型".equals(yourType)){
            return getColor(R.color.colorPuple);
        }else if("理智型".equals(yourType)){
            return getColor(R.color.colorGray);
        }else if("疑惑型".equals(yourType)){
            return getColor(R.color.colorPrimary);
        }else if("活跃型".equals(yourType)){
            return getColor(R.color.colorOrange);
        }else if("领袖型".equals(yourType)){
            return getColor(R.color.colorRed);
        }else if("平和型".equals(yourType)){
            return getColor(R.color.colorYellow);
        }else{

        }
        return getColor(R.color.colorWhite);
    }

    @Override
    public void onValueSelected(Entry entry, Highlight highlight) {

    }

    @Override
    public void onNothingSelected() {

    }
}
